package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.*;
import cn.tedu.property.management.pojo.dto.ChargeItemAddNewDTO;
import cn.tedu.property.management.pojo.dto.ChargeItemUpdateDTO;
import cn.tedu.property.management.pojo.vo.*;
import cn.tedu.property.management.pojo.entity.ChargeItem;
import cn.tedu.property.management.service.IChargeItemService;
import cn.tedu.property.management.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 处理费用项目的业务实现类
 *
 * @author zyl
 * @version 0.0.1
 */
@Service
@Slf4j
public class ChargeItemServiceImpl implements IChargeItemService {

    @Autowired
    private ChargeDetailMapper chargeDetailMapper;

    @Autowired
    private ChargeItemMapper chargeItemMapper;

    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Autowired
    private CommunityMapper communityMapper;


    @Override
    public void addNew(ChargeItemAddNewDTO chargeItemAddNewDTO) {
        log.debug("开始处理添加费用详情的功能！参数为：{}",chargeItemAddNewDTO);
        //判断是否有改业主
        if (ownerMapper.getOwnerById(chargeItemAddNewDTO.getOwnerId())==null){
            String massage = "添加失败，没有该业主！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        //判断收费项目名称是否重复
        if (chargeItemMapper.selectByName(chargeItemAddNewDTO.getName()) != null){
            String massage = "添加失败，改费用以存在！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        ChargeItem chargeItem = new ChargeItem();
        BeanUtils.copyProperties(chargeItemAddNewDTO , chargeItem);
        chargeItemMapper.addNew(chargeItem);
        log.debug("添加完成！");
    }

    @Override
    @Scheduled(cron = "0 0 1 1 * ?")
    public void addNewTeam() {
        log.debug("开始自动生成物业费！");
        List<OwnerListVO> allOwner = ownerMapper.getAllOwner();
        List<CommunityStandardVO> community = communityMapper.getCommunity();
        Long communityId = 0L;
        String communityName = "";
        // 遍历小区，获得小区名称
        for (CommunityStandardVO communityStandardVO : community) {
            communityId = communityStandardVO.getId();
            communityName = communityStandardVO.getName();
        }
        // 遍历业主，获得业主信息
        for (OwnerListVO ownerListVO : allOwner) {
            Long ownerId = ownerListVO.getId();
            List<HouseListVO> list = houseMapper.getListByOwnerId(ownerId);
            // 遍历房屋，获得房屋信息
            for (HouseListVO houseListVO : list) {
                ChargeItem chargeItem = new ChargeItem();
                chargeItem.setCommunityId(communityId);
                chargeItem.setCommunityName(communityName);
                chargeItem.setOwnerId(ownerId);
                chargeItem.setMoney(houseListVO.getExpense());
                LocalDateTime dateTime = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-dd");
                chargeItem.setName(dateTime.format(formatter)+"月物业费");
                chargeItemMapper.addNew(chargeItem);
            }
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理添加费用详情的功能！参数为：{}",id);
        //判断是否有该id的费用项目
        if (chargeItemMapper.selectById(id) == null){
            String massage = "删除失败，该费用项目不存在！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        chargeItemMapper.delete(id);
        log.debug("删除完成！");
    }

    @Override
    public void updateById(Long id, ChargeItemUpdateDTO chargeItemUpdateDTO) {
        log.debug("开始处理修改费用详情的功能！id为：{}，参数为：{}",id,chargeItemUpdateDTO);
        //判断是否有该id的费用项目
        if (chargeItemMapper.selectById(id) == null){
            String massage = "修改失败，该费用项目不存在！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        //判断收费项目名称是否重复
        if (chargeItemMapper.selectByName(chargeItemUpdateDTO.getName()) != null){
            String massage = "修改失败，该费用以存在！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        chargeItemMapper.updateById(id,chargeItemUpdateDTO);
        log.debug("修改完成！");
    }

    @Override
    public ChargeItemStandardVO selectById(Long id) {
        log.debug("开始处理根据id查询费用详情的功能！参数为：{}",id);
        //判断是否有该id的费用项目
        if (chargeItemMapper.selectById(id) == null){
            String massage = "查询失败，该费用项目不存在！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        ChargeItemStandardVO chargeItemStandardVO = chargeItemMapper.selectById(id);
        log.debug("查询完成！");
        return chargeItemStandardVO;
    }

    @Override
    public ChargeItemStandardVO selectByName(String name) {
        log.debug("开始处理根据名字查询费用详情的功能！参数为：{}",name);
        //判断是否有该id的费用项目
        if (chargeItemMapper.selectByName(name) == null){
            String massage = "查询失败，该费用项目不存在！";
            log.debug(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT , massage);
        }
        ChargeItemStandardVO chargeItemStandardVO = chargeItemMapper.selectByName(name);
        log.debug("查询完成！");
        return chargeItemStandardVO;
    }

    @Override
    public List<ChargeItemListItemVO> selectList() {
        log.debug("开始处理批量查询费用详情的功能！无参数！");
        List<ChargeItemListItemVO> chargeItemListItemVO = chargeItemMapper.selectList();
        return chargeItemListItemVO;
    }

    @Override
    public List<ChargeItemListItemVO> selectByCreateTime(LocalDateTime createTime) {
        log.debug("开始处理根据时间查询费用详情的功能！参数为：{}！",createTime);
        List<ChargeItemListItemVO> chargeItemListItemVO = selectByCreateTime(createTime);
        log.debug("查询完成！");
        if (chargeItemListItemVO == null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT , "没有该时间的费用项目！");
        }else {
            return chargeItemListItemVO;
        }
    }

    @Override
    public List<ChargeItemListItemVO> selectByOwnerId(Long ownerId) {
        log.debug("开始处理根据业主id查询费用详情的功能！参数为：{}！",ownerId);
        List<ChargeItemListItemVO> chargeItemListItemVO = selectByOwnerId(ownerId);
        log.debug("查询完成！");
        if (chargeItemListItemVO == null){
            throw new ServiceException(ServiceCode.ERR_CONFLICT , "没有该业主的费用项目！");
        }else {
            return chargeItemListItemVO;
        }
    }


}
